package com.scrx.ymg.service.agent;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.scrx.ymg.dao.agent.IAgentInfoDao;
import com.scrx.ymg.dao.product.IProductDao;
import com.scrx.ymg.dao.seckill.ISeckillDao;
import com.scrx.ymg.dao.supermarket.ISuperMarketDao;
import com.scrx.ymg.dao.user.IUserDao;
import com.scrx.ymg.entity.AgentHot;
import com.scrx.ymg.entity.AgentInfo;
import com.scrx.ymg.entity.AgentMoney;
import com.scrx.ymg.entity.AgentSuperMarketSet;
import com.scrx.ymg.entity.BankAccount;
import com.scrx.ymg.entity.OtherInfo;
import com.scrx.ymg.entity.OtherMoney;
import com.scrx.ymg.entity.SuperMarketInfo;
import com.scrx.ymg.entity.SupermarketSet;
import com.scrx.ymg.entity.User;
import com.scrx.ymg.entity.UserComplain;
import com.scrx.ymg.util.BaseUtil;
import com.scrx.ymg.util.LatLonUtil;

@Service("agentService")
public class AgentServiceImpl implements IAgentService {

	@Autowired
	private IAgentInfoDao agentDao;

	@Autowired
	private ISuperMarketDao superDao;
	
	@Autowired
	private IProductDao   productDao;
	
	@Autowired
	private ISeckillDao seckilldao;
	
	@Autowired
	private IUserDao userdao;

	public String saveAgentHot(AgentHot hot) {
		return agentDao.saveAgentHot(hot);
	}

	public String deleteHotProduct(int hotId) {
		AgentHot hot = agentDao.selectAgentHot(hotId);
		return agentDao.delectAgentHot(hot);
	}

	public Map<Object, Object> getHotProduct(AgentHot hot, int pageNum, int pageCount) {

		return agentDao.selectAgentHot(hot, pageNum, pageCount);
	}

	public String saveAgentHot(AgentHot hot, MultipartFile thumbnail, MultipartFile banner,MultipartFile bigpicFile,MultipartFile hotDetailFile) {
		LatLonUtil util = new LatLonUtil();
		String hotsPath = BaseUtil.hotsPath;
		
		String hotDetailFileName=BaseUtil.addRandom(hotDetailFile.getOriginalFilename());
		String hotDetailFilePath = hotsPath + hotDetailFileName;
		hot.setHotDetailImg(hotDetailFileName);
		
		String thumbnailName=BaseUtil.addRandom(thumbnail.getOriginalFilename());
		String thumbnailPath = hotsPath + thumbnailName;
		hot.setProductSimg(thumbnailName);
		
		String bannerName=BaseUtil.addRandom(banner.getOriginalFilename());
		String bannerPath = hotsPath + bannerName;
		hot.setAdvertisement(bannerName);
		
		String bigpicFileName=BaseUtil.addRandom(bigpicFile.getOriginalFilename());
		String bigpicPaht = hotsPath+bigpicFileName;
		hot.setProductBimg(bigpicFileName);
		
		hot.setChecka(0);
		hot.setPutaway(1);
		hot.setSubmitTime(util.newdate());
		String msg = agentDao.saveAgentHot(hot, thumbnail, thumbnailPath, banner, bannerPath,
				hotDetailFile,hotDetailFilePath,bigpicFile,bigpicPaht);
		return msg;
	}

	public String createSuperMarket(SuperMarketInfo superInfo, Integer agentId, MultipartFile liveAction,
			MultipartFile license, MultipartFile deliver, MultipartFile card) {
		//AgentInfo agent = agentDao.getAgentInfoByID(superInfo.getAgentId());
        
		// 判断超市账号,超市名称是否重复
		SuperMarketInfo infoName = agentDao.superNameExists(superInfo.getName());
		SuperMarketInfo infoAccount = agentDao.accountExists(superInfo.getAccount());
		if(infoName==null&&infoAccount==null){
			String supersPath = BaseUtil.supersPath;
			//superInfo.setAgentRegion(agent.getAgentRegion());
			//superInfo.setRemark("新建");
			// 实景
			String liveActionName=BaseUtil.addRandom(liveAction.getOriginalFilename());
			superInfo.setLiveAction(liveActionName);
			String liveActionPath = supersPath + liveActionName;
			// 执照
//			String licenseName=BaseUtil.addRandom(license.getOriginalFilename());
//			superInfo.setLicense(licenseName);
//			String licensePath = supersPath + licenseName;
//			// 送货人员
//			String deliverName=BaseUtil.addRandom(deliver.getOriginalFilename());
//			superInfo.setCourier(deliverName);
//			String deliverPath = supersPath + deliverName;
//			// 身份证
//			String cardName=BaseUtil.addRandom(card.getOriginalFilename());
//			superInfo.setIdentityCardImg(cardName);
//			String cardPath = supersPath + cardName;
			//超市状态 营业中
			superInfo.setStatus(5);
			//支付方式 货到付款
			superInfo.setPaymentMode(1);
			SupermarketSet set=new SupermarketSet();
			set.setUpSendMoney(10.0);
			set.setFreeSendMoney(38.0);
			set.setSendMoney(2.0);
			set.setSendPeople("送货员");
			set.setSendPhone(superInfo.getPhone());
			set.setSendTime("9:00-21:00");
			set.setAfterService("全心全意为顾客服务!");
			set.setStatus(2);
			set.setAgentId(agentId);
			set.setSuperName(superInfo.getName());
			String msg = superDao.createSuperMarket(superInfo,set, liveAction, liveActionPath, license, "", deliver,
					"", card, "");
			return msg;
		}else{
			return "251";
		}
	}
	
	public String updateSuperMarket(SuperMarketInfo superInfo, Integer agentId, MultipartFile liveAction,
			MultipartFile license, MultipartFile deliver, MultipartFile card) {
		//AgentInfo agent = agentDao.getAgentInfoByID(superInfo.getAgentId());

		// 判断超市账号,超市名称是否重复
		//SuperMarketInfo infoName = agentDao.superNameExists(superInfo.getName());
		//SuperMarketInfo infoAccount = agentDao.accountExists(superInfo.getAccount());
		//if(infoName==null){
			String supersPath = BaseUtil.supersPath;
			//superInfo.setAgentRegion(agent.getAgentRegion());
			//User user =userdao.findById(Integer.parseInt(superInfo.getRemark()));
			//User user = users.findById(Integer.parseInt(bank.getOpeningBank()));
//			 BankAccount  bank = superDao.isNullBankAccountInfo(superInfo.getId());
//			 if(bank==null){
//				 bank=new BankAccount();
//			 }
//			 bank.setAccountName(user.getUserName());
//			 bank.setIdCard(user.getUserId());
//			 bank.setSuperId(superInfo.getId());
//			 bank.setBankCard(superInfo.getPhone());
//			 bank.setOpeningBank(user.getId().toString());

			// superInfo.setRemark("新建");
			// 实景
			String liveActionName=BaseUtil.addRandom(liveAction.getOriginalFilename());
			superInfo.setLiveAction(liveActionName);
			String liveActionPath = supersPath + liveActionName;
//			// 执照
//			String licenseName=BaseUtil.addRandom(license.getOriginalFilename());
//			superInfo.setLicense(licenseName);
//			String licensePath = supersPath + licenseName;
//			// 送货人员
//			String deliverName=BaseUtil.addRandom(deliver.getOriginalFilename());
//			superInfo.setCourier(deliverName);
//			String deliverPath = supersPath + deliverName;
//			// 身份证
//			String cardName=BaseUtil.addRandom(card.getOriginalFilename());
//			superInfo.setIdentityCardImg(cardName);
//			String cardPath = supersPath + cardName;
			//超市状态 营业中
			//superInfo.setStatus(0);
			//支付方式 货到付款
//			superInfo.setPaymentMode(1);
//			SupermarketSet set=productDao.getSupermarketSetBySuperID(superInfo.getId());
//			set.setUpSendMoney(10.0);
//			set.setFreeSendMoney(38.0);
//			set.setSendMoney(2.0);
//			set.setSendPeople("送货员");
//			set.setSendPhone(superInfo.getPhone());
//			set.setSendTime("9:00-21:00");
//			set.setAfterService("全心全意为顾客服务.");
//			set.setStatus(2);
//			set.setAgentId(agentId);
//			set.setSuperName(superInfo.getName());
			String msg = superDao.updateSuperMarket(superInfo,null,null, liveAction, liveActionPath, license, "", deliver,
					"", card, "");
			return msg;
		//}else{
			//return "251";
		//}
	}

	public Map<Object, Object> getSuperMarketSetinfo(AgentSuperMarketSet set, int pageNum, int pageCount) {
		return superDao.getSuperMarketSetinfo(set, pageNum, pageCount);
	}


	public String updateAgentHot(AgentHot hot, MultipartFile thumbnail, MultipartFile banner,MultipartFile bigpicFile,MultipartFile hotDetailFile) {
		LatLonUtil util = new LatLonUtil();
		String hotsPath = BaseUtil.hotsPath;
		String thumbnailPath="";
		String bannerPath ="";
		String hotDetailFilePath ="";
		String bigpicFilePath="";
		AgentHot agentHot = agentDao.getHotProduct(hot.getId());
		if(agentHot.getChecka()==1){
			return "false";
		}
 
		if(hotDetailFile.getSize()>0){
			String hotDetailFileName=BaseUtil.addRandom(hotDetailFile.getOriginalFilename());
			hotDetailFilePath = hotsPath + hotDetailFile.getOriginalFilename();
			hot.setHotDetailImg(hotDetailFileName);
		}else{
			hot.setHotDetailImg(agentHot.getHotDetailImg());
		}
		
		
		if(thumbnail.getSize()>0){
			String thumbnailName=BaseUtil.addRandom(thumbnail.getOriginalFilename());
			thumbnailPath = hotsPath + thumbnailName;
			hot.setProductSimg(thumbnailName);
		}else{
			hot.setProductSimg(agentHot.getProductSimg());
		}
		
		if(banner.getSize()>0){
			String bannerName=BaseUtil.addRandom(banner.getOriginalFilename());
			bannerPath = hotsPath + bannerName;
			hot.setAdvertisement(bannerName);
		}else{
			hot.setAdvertisement(agentHot.getAdvertisement());
		}
		
		if(bigpicFile.getSize()>0){
			String bigpicFileName=BaseUtil.addRandom(bigpicFile.getOriginalFilename());
			bigpicFilePath = hotsPath + bigpicFileName;
			hot.setProductBimg(bigpicFileName);
		}else{
			hot.setProductBimg(agentHot.getProductBimg());
		}
			
		hot.setSubmitTime(util.newdate());
		String msg = agentDao.updateAgentHot(hot, thumbnail, thumbnailPath, banner, bannerPath
				,hotDetailFile,hotDetailFilePath,bigpicFile,bigpicFilePath);
		return msg;
	}


	public String agreeOrNotSuperMarketSet(AgentSuperMarketSet sueprset) {
		AgentSuperMarketSet oldset=productDao.getAgentSuperMarketSetBySuperID(sueprset.getSupermarketId());
		SupermarketSet set=productDao.getSupermarketSetBySuperID(sueprset.getSupermarketId());
		SuperMarketInfo info = superDao.findByID(sueprset.getSupermarketId());
		if(oldset!=null){
			oldset.setStatus(sueprset.getStatus());
			if(sueprset.getStatus()==2){
			set.setUpSendMoney(oldset.getUpSendMoney());
	    	set.setFreeSendMoney(oldset.getFreeSendMoney());
	    	set.setSendMoney(oldset.getSendMoney());
	    	set.setSendPeople(oldset.getSendPeople());
	    	set.setSendPhone(oldset.getSendPhone());
	    	set.setSendTime(oldset.getSendTime());
	    	set.setAfterService(oldset.getAfterService());
	    	set.setSuperName(oldset.getSuperName());
	    	info.setName(oldset.getSuperName());
			return productDao.updateAgentSuperMarketSet(oldset,set,info);
			}else{
				return productDao.updateAgentSuperMarketSet(oldset);
			}
		}
		return "false";
	}

	public Map<Object, Object> getSuperMarketInfoList(SuperMarketInfo info, int pageNum, int pageCount) {
		
		return agentDao.getSuperMarketInfoList(info, pageNum, pageCount);
	}


	public AgentMoney getAgentMoneyInfo(int agentId) {

		return agentDao.getAgentMoneyInfo(agentId);
	}


	public Map<Object, Object> getUserComplaint(UserComplain complain,String startTime,String endTime, int pageNum, int pageCount) {

		return agentDao.getUserComplaint(complain, startTime, endTime,pageNum,pageCount);
	}


	public Map<Object, Object> statisticsSuperMarketInfo(SuperMarketInfo info, int pageNum, int pageCount) {

		return  agentDao.statisticsSuperMarketInfo(info, pageNum, pageCount);
	}


	public String addAgentInfo(AgentInfo info) {
		AgentInfo oldinfo = agentDao.getAgentInfoByagentRegion(info.getAgentRegion());
		AgentInfo infoAccount = agentDao.getAgentInfoBytAccount(info.getAccount());
		String msg="false";
		if(oldinfo==null&&infoAccount==null){
			info.setStatus(1);
			info.setPaymentMode(1);
			AgentMoney money =new AgentMoney();
			money.setAccountMoney(0.0);
			money.setAgentAdMoney(0.0);
			money.setDirectMoney(0.0);
			money.setGroupMoney(0.0);
			money.setTotalMoney(0.0);
			return agentDao.addAgentInfo(info,money);
		}else{
		    return "250";
		}
	}
	
    public String updateAgentInfo(AgentInfo info, String uUID) {
    	AgentInfo oldinfo = agentDao.getAgentInfoByID(info.getId());
		AgentInfo infoAccount = agentDao.getAgentInfoBytAccount(info.getAccount());
		if(infoAccount==null){
			BankAccount  bank = superDao.isNullBankAccountInfoAgent(info.getId());
			info.setAgentRegion(oldinfo.getAgentRegion()); 
			info.setStatus(1);
			info.setPaymentMode(1);
			User user = userdao.findById(Integer.parseInt(uUID));
			 if(bank!=null){
				 if(user!=null){
					 bank.setAccountName(user.getUserName());
					 bank.setIdCard(user.getUserId());
					 bank.setAgentId(info.getId());
					 bank.setBankCard(info.getPhone());
					 bank.setOpeningBank(user.getId().toString());
				 }
			 }else{
				 if(user!=null){
				 BankAccount  newbank= new BankAccount();
				 newbank.setAccountName(user.getUserName());
				 newbank.setIdCard(user.getUserId());
				 newbank.setAgentId(info.getId());
				 newbank.setBankCard(info.getPhone());
				 newbank.setOpeningBank(user.getId().toString());
				 return agentDao.updateAgentInfoSaveBank(info,newbank);
				 }
			 }

			return agentDao.updateAgentInfo(info,bank);
		}else{
		    return "250";
		}
	}


	public Map<Object, Object> selectAgentInfoList(AgentInfo info, int pageNum, int pageCount) {

		return agentDao.selectAgentInfoList(info,pageNum,pageCount);
	}

	
	public String changeStatusAgentInfo(AgentInfo info) {
		AgentInfo oldinfo = agentDao.getAgentInfoByID(info.getId());
		String msg="false";
               if(oldinfo!=null){
            	   oldinfo.setStatus(info.getStatus());
            	   msg=agentDao.updateAgentInfo(oldinfo);
               }
		return msg;
	}


	public Map<Object, Object> getHotProduct(AgentHot hot, int pageNum, int pageCount, String agentRegion) {
		if(agentRegion!=null&&agentRegion!=""){
		AgentInfo oldinfo = agentDao.getAgentInfoByagentRegion(agentRegion);
		if(oldinfo!=null){
		hot.setAgentId(oldinfo.getId());
		}else{
			hot.setAgentId(-1);
		}
		}
		return agentDao.selectAgentHotHead(hot, pageNum, pageCount);
	}


	public String insertOtherInfo(OtherInfo info, MultipartFile sjsj, MultipartFile yyzz, MultipartFile htxy,
			MultipartFile card) {
		// 判断超市账号,超市名称是否重复
		OtherInfo oldinfo = agentDao.OtherInfoExists(info);

				if(oldinfo==null){
					String othersPath = BaseUtil.othersPath;
					// 实景
					String  sjsjName=BaseUtil.addRandom(sjsj.getOriginalFilename());
					info.setLiveAction(sjsjName);
					String liveActionPath = othersPath + sjsjName;
					// 执照
					String  yyzzName=BaseUtil.addRandom(yyzz.getOriginalFilename());
					info.setLicense(yyzzName);
					String licensePath = othersPath + yyzzName;
					// 送货人员
					String  htxyName=BaseUtil.addRandom(htxy.getOriginalFilename());
					info.setCourier(htxyName);
					String deliverPath = othersPath + htxyName;
					// 身份证
					String  cardName=BaseUtil.addRandom(card.getOriginalFilename());
					info.setIdentityCardImg(cardName);
					String cardPath = othersPath + cardName;
					
					OtherMoney money =new OtherMoney();
					money.setAccountMoney(0.0);
					money.setTotalMoney(0.0);
		            
					String msg = agentDao.createOtherInfo(info, sjsj, liveActionPath, yyzz, licensePath, htxy,
							deliverPath, card, cardPath,money);
					return msg;
				}else{
					return "250";
				}
	}


	public String updateOtherInfo(OtherInfo info) {
		OtherInfo oldother = seckilldao.getOtherInfoById(info.getId());
		oldother.setOtherStatus(info.getOtherStatus());
		oldother.setRemark(info.getRemark());
		return seckilldao.updateOtherInfo(oldother);
	}


	public AgentInfo getAgentInfoByagentRegion(String agentRegion) {
		return agentDao.getAgentInfoByagentRegion(agentRegion);
	}


	public AgentInfo getAgentInfoByagentRegionAndStatus(String agentRegion) {
		return agentDao.getAgentInfoByagentRegionAndStatus(agentRegion);
	}


	public AgentInfo getAgentInfo(int agentId) {
		return agentDao.getAgentInfoByID(agentId);
	}


	public String putawayHotProduct(int hotId, int putaway) {
		AgentHot hot = agentDao.selectAgentHot(hotId);
		hot.setPutaway(putaway);
		return agentDao.updateAgentHotAndSuperEntrepot(hot);
	}


	


}
